<template>
  <div class="app-container">
    <div class="form-box">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-tabs type="border-card" class="form-tabs">
          <el-tab-pane :label="$t('common.edit_info')" class="form-tabs-tab-pane">
            <!-- 上一级 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('treeTable.parent_id')"
                  prop="parent_id"
                >
                  <el-cascader
                    v-model="parent_ids"
                    :options="parentData"
                    :props="{ checkStrictly: true, children: 'children', label: 'name', value: 'id'}"
                    :placeholder="$t('treeTable.parent_id_tips')"
                    filterable
                    clearable
                    @change="handleParentChange"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 名称 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.name')" prop="name">
                  <el-input v-model="form.name" :placeholder="$t('common.name_tips')" clearable />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 发布状态 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.status')"
                  prop="status"
                >
                  <el-select
                    v-model.number="form.status"
                    :placeholder="$t('common.pleaseSelect')+$t('common.status')"
                    clearable
                  >
                    <el-option
                      v-for="item in dictionaryService.getItems('statusVar')"
                      :key="item.id"
                      :label="item.name"
                      :value="Number(item.value)"
                    />

                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>

        <el-row :gutter="20" type="flex" justify="center" class="btn-row">
          <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
            <el-form-item>
              <el-button type="primary" :loading="submitLoading" @click="onSubmit">{{ $t('common.submit') }}</el-button>
              <el-button @click="onCancel">{{ $t('common.cancel') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { show, update, store, getParentTreeConfig } from '@/api/example/tree-table'
import { loadingStart } from '@/utils/loading-service'
import { mapGetters } from 'vuex'

// export
export default {
  name: 'TreeTableEdit',
  components: {
  },
  data() {
    return {
      submitLoading: false,
      form: {
        parent_id: 0,
        name: '',
        cover_image: 0,
        keywords: [],
        description: '',
        sort: 999,
        status: 1
      },
      cover_imageList: [
      ],
      // 表单输入校验 ，通过prop="name"属性进行控制
      rules: {
        name: [
          { required: true, message: this.$t('treeTable.name_tips'), trigger: 'blur' },
          { min: 2, max: 50, message: this.$t('treeTable.name_length_tips'), trigger: 'blur' }
        ]
      },
      // 上级数据
      parentData: [],
      parent_ids: [],
      // 路由参数
      routerParams: {},
      back_route_name: 'TreeTableIndex'
    }
  },
  computed: {
    ...mapGetters(['fileUploadConfig'])
  },
  created() {
    this.routerParams = this.$route.params
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    // 内容详情
    async getDetail() {
      const loading = loadingStart()
      const { id } = this.routerParams
      if (id > 0) {
        await show(id).then((response) => {
          for (const key in response.data) {
            if (key in this.form) {
              this.form[key] = response.data[key]
            }
          }
        }).catch(() => {
          this.closeBack(this.back_route_name)
        })
      }

      // 上级
      await getParentTreeConfig((id > 0 ? id : 0)).then((response) => {
        const { items, parent_ids } = response.data
        this.parentData = items
        this.parent_ids = parent_ids
      }).catch(() => {})

      loading.close()
    },
    onSubmit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.submitLoading = true
          let response
          if (this.routerParams.id) {
            response = await update(this.routerParams.id, this.form).catch(() => {})
          } else {
            response = await store(this.form).catch(() => {})
          }
          if (response instanceof Object) {
            this.$message.success(response.message)
            this.closeBack(this.back_route_name)
          }
          this.submitLoading = false
        } else {
          this.$message.error(this.$t('common.pleaseFillRequired'))
          return false
        }
      })
    },
    onCancel() {
      this.closeBack(this.back_route_name)
    },
    handleImageUploadChange(result) {
      this.form.cover_image = result.ids
    },
    // 选择上级
    handleParentChange(e) {
      this.form.parent_id = e && e.length > 0 ? e[e.length - 1] : 0
    }
  }
}
</script>

<style scoped lang="scss">
</style>
